<template>
  <div>
    {{  pageModel }}
    <el-table :data="tableData"
              border
              align="center"
              header-align="center"
              width="100%"
              style="text-align: center;"
              tooltip-effect="dark">
      <el-table-column type="index"
                       width="50">
      </el-table-column>
      <el-table-column prop="id"
                       label="奖品id"
                       width="80">
      </el-table-column>
      <el-table-column prop="prizeName"
                       label="奖品名字">
      </el-table-column>
      <el-table-column prop="prizeClawType"
                       label="图片类型"
                       v-if="doll">
      </el-table-column>
      <el-table-column prop="prizeNum"
                       label="奖品数量">
      </el-table-column>
      <el-table-column prop="prizeProbability"
                       label="中奖概率(%)">
      </el-table-column>
      <el-table-column prop="prizeType"
                       :formatter="typeFormat"
                       label="奖品类型">
      </el-table-column>
      <el-table-column prop="prizeGrade"
                       label="奖品等级">
      </el-table-column>
      <el-table-column prop="prizeSort"
                       label="奖品顺序">
      </el-table-column>
      <el-table-column width="200"
                       label="操作">
        <template slot-scope="scope">
          <el-button class="pan-btn green-btn"
                     @click="delHandle(scope.row, scope.$index)">删除</el-button>
          <el-button class="pan-btn green-btn"
                     @click="editHandle(scope.row, scope.$index)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>

</template>
<script>
import request from '@/utils/request'
export default {
  name: 'prizeList',
  props: ['json'],
  data() {
    return {
      doll: false,
      tableData: []
    }
  },
  mounted() {
    if (this.json.templateMark === 'prizeclaw') {
      this.doll = true
    } else {
      this.doll = false
    }
  },
  computed: {
    pageModel: function() {
      if (this.json.pageModel == 'prizeList') {
        const _this = this
        request({
          url: '/activityConsole/activity/activityInfoId/' + _this.json.activityId + '/prize',
          method: 'get'
        }).then(res => {
          console.log(res)
          for (var i = 0; i < res.data.length; i++) {
            if (res.data[i].prizeClawType === '') {
              res.data[i].prizeClawType = ''
            }
            if (res.data[i].prizeClawType === 'low') {
              res.data[i].prizeClawType = '低配'
            }
            if (res.data[i].prizeClawType === 'high') {
              res.data[i].prizeClawType = '高配'
            }
          }

          //   console.log(res.data)
          return _this.tableData = res.data
        }).catch(err => {
          console.log(err)
        })
      }
    }
  },
  methods: {
    editHandle(row) {
      this.$emit('listenPrizeEdit', { hideSetFlag: true, pageModel: 'prizeSet', prizeId: row.id })
    },
    delHandle(row, index) {
      const _this = this
      this.$confirm('此操作将永久删除该奖品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'green-btn pan-btn',
        type: 'warning'
      }).then(() => {
        request({
          url: '/activityConsole/activity/activityInfoId/' + row.activityInfoId + '/prize/id/' + row.id,
          method: 'DELETE'
        }).then(res => {
          if (res.status == '200') {
            _this.tableData.splice(index, 1)
          }
          _this.$message({
            type: 'success',
            message: res.description
          })
        }).catch(err => {
          console.log(err)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    typeFormat(row, column) {
      const type = row[column.property]
      if (type == 'RECHARGE_CARD') {
        return '充值卡'
      }
      if (type == 'FLOW_PACKAGE') {
        return '流量包'
      }
      if (type == 'ELECTRONIC_COUPON') {
        return '优惠券'
      }
      if (type == 'ENTITY_GIFFT') {
        return '实物'
      }
      if (type == 'THANKS_PARTICIPATE') {
        return '谢谢参与'
      }
      if (type == 'CLUB_INTEGRAL') {
        return '积分'
      }
      if (type == 'QY_RECHARGE') {
        return '权益会员直充'
      }
    }
  }
}
</script>
